#!/bin/sh

op=$1

os=`uname -s`
awkc=awk
if [ "$os" = "SunOS" ]; then
   awkc=nawk
fi

orauser=${LB_ORACLE_USER}
if [ -z "$orauser" ]; then
    orauser=`ps -ef | grep ora_pmon | grep -v "grep" | head -n1 | sed -e 's/^[ \t]*//' | cut -d" " -f1`
fi
if [ -z "$orauser" ];then
    orauser=oracle
fi

user_id=`id ${orauser}`
if [ -z "$user_id" ];then
    echo "can't find user: ${orauser}"
    exit 1
fi

oraclehome=${LB_ORACLE_HOME}
if [ -z "$oraclehome" ];then
    oraclehome=`cat /etc/oratab | grep -v "^#" | grep -v "^\+" | grep ".*:.*:[Y:N]" | head -n1 | cut -d":" -f2`
fi
if [ -z "$oraclehome" ];then
    oraclehome=`ps -ef | grep tns | grep -v "grep" | grep -i listener | $awkc NR==1 | $awkc -F"/bin" '{print $(NF-1)}' | $awkc '{print $NF}'`
fi
if [ -z "$oraclehome" ];then
    oraclehome=`su - $orauser -c "echo \$ORACLE_HOME"`
fi

listener=${oraclehome}/network/admin

group=`id -gn ${orauser}`

scriptsRoot=${LB_ROOT}
if [ -z "$scriptsRoot" ]; then
  scriptsRoot=/opt/howlink/linkBackup/resource/scripts
  export LB_ROOT=$scriptsRoot
fi

echo_help()
{
    echo "Usage: $0 {create osid sid <target> <rtime> rlog}"
    echo "       $0 {delete sid <target> <rtime> rlog}"
}

validate_sid()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_target()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi

    if [ ! -e "$a" ];then
        echo "$a is not exists"
        exit 1
    fi
}

validate_store()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi

    if [ ! -e "$a" ];then
        echo "$a is not exists"
        exit 1
    fi
}

validate_rtime()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}

validate_rlog()
{
    a=$1
    if [ -z "$a" ];then
        echo_help
        exit 1
    fi
}


create()
{
    osid=$1
    sid=$2
    target=$3
    store=$4
    rtime=$5
    rlog=$6
    params=$7

    chown -R ${orauser}:${group} ${target}
    chown -R ${orauser}:${group} ${scriptsRoot}/oracle/oracleclone
    if [ ! -e "$rlog" ];then
        touch ${rlog}
    fi
    chown ${orauser}:${group} ${rlog}

    hostip="localhost"
    su - ${orauser} << EOF
osid=$osid
sid=$sid
hostip=$hostip
orahome=${oraclehome}
tnsadmindir=${listener}
username=${orauser}
notnsupdate=false;
nonid=false;
rrecovery=true;
isrestore=false;
isgrandchild=false;
isremount=false;
ischild=false;
opname=mount;
LB_ROOT=${scriptsRoot}
LB_PARAM=${params}

export sid;
export nonid;
export notnsupdate;
export hostip
export orahome;
export rrecovery;
export tnsadmindir;
export username;
export isrestore;
export isgrandchild;
export isremount;
export ischild;
export opname;
export LB_ROOT
export LB_PARAM

${scriptsRoot}/oracle/oracleclone/OracleAppMount_tstamp.sh ${osid} ${sid} ${oraclehome} ${target} ${rtime} >> ${rlog} 2>> ${rlog}
EOF

}

delete()
{
    sid=$1
    target=$2
    store=$3
    rlog=$4

    if [ ! -e "$rlog" ];then
        touch ${rlog}
    fi
    chown ${orauser}:${group} ${rlog}

    ${scriptsRoot}/oracle/oracleclone/OracleAppTeardown.sh ${sid} ${oraclehome} ${orauser} >> ${rlog} 2>> ${rlog}
}

case ${op} in
create)
    osid=$2
    sid=$3
    target=$4
    store=$5
    rtime=$6
    rlog=$7
    params=$8

    validate_sid ${osid}
    validate_sid ${sid}
    validate_target ${target}
    validate_store ${store}
    validate_rtime ${rtime}
    validate_rlog ${rlog}

    create ${osid} ${sid} ${target} ${store} ${rtime} ${rlog} ${params}
    ;;
delete)
    sid=$2
    target=$3
    store=$4
    rlog=$5

    validate_sid ${sid}
    validate_target ${target}
    validate_store ${store}
    validate_rlog ${rlog}

    delete ${sid} ${target} ${store} ${rlog}
    cat /etc/oratab | sed "/${sid}:.*:Y/d" > /etc/oratab
    ;;
*)
    echo_help
    exit 1
esac